﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicViet.Models;

namespace MedicViet.Controllers
{
    public class NewsController : Controller
    {
        private MedicVietEntities db = new MedicVietEntities();
        private DiskFileStore _fileStore = new DiskFileStore();
        
        public ViewResult Index()
        {
            var listNew = (from c in db.mvCategories.Where(cat => cat.name == "News")
                           from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                           orderby p.dateCreate
                           descending
                           select p).ToList<mvNew>();
            return View(listNew.ToList());
        }

        public ViewResult Details(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            return View(mvnew);
        }


        public ActionResult TinTucYHoc()
        {
            var ktyh = (from c in db.mvCategories.Where(cat => cat.name == "TinTucYHoc")
                         from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                         orderby p.dateCreate
                         descending
                        select p).ToList<mvNew>() ;
            return View(ktyh);
        }
        public ActionResult ThongTinSucKhoe()
        {
            var ktyh = (from c in db.mvCategories.Where(cat => cat.name == "TinTucYHoc")
                        from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                        orderby p.dateCreate
                        descending
                        select p).ToList<mvNew>();
            return View(ktyh);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(HttpPostedFileBase thumbPath, int catName, string fileName, mvNew mvnew, String nContent)
        {
            Guid? thumb = null;
            if (thumbPath != null)
            {
                thumb = _fileStore.SaveUploadedFile(Request.Files["thumbPath"]);
                fileName = Request.Files["thumbPath"].FileName;
            }
            if (ModelState.IsValid)
            {
                mvCategory cat = (from C in db.mvCategories
                                  where C.id == catName
                                  select C).FirstOrDefault();
                mvnew.thumbPath = thumb.Value + ".png";
                mvnew.dateCreate = DateTime.Now;
                mvnew.mvCategories.Add(cat);
                mvnew.userId = 1;
                db.mvNews.AddObject(mvnew);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }

        //[HttpPost]
        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Create(Guid? thumbPath, string fileName, mvNew mvnew)
        //{
        //    if (Request.Files["thumbPath"] != null)
        //    {
        //        thumbPath = _fileStore.SaveUploadedFile(Request.Files["thumbPath"]);
        //        fileName = Request.Files["thumbPath"].FileName;
        //    }
        //    if (ModelState.IsValid)
        //    {
        //        mvCategory cat = (from C in db.mvCategories
        //                          where C.name == "News"
        //                          select C).FirstOrDefault();
        //        mvnew.thumbPath = Request.Form["thumbPath"] + ".png";
        //        mvnew.dateCreate = DateTime.Now;
        //        mvnew.mvCategories.Add(cat);
        //        mvnew.userId = 1;
        //        db.mvNews.AddObject(mvnew);
        //        db.SaveChanges();

        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
        //    return View(mvnew);
        //}

        public ActionResult Edit(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }

        [HttpPost]
        public ActionResult Edit(mvNew mvnew)
        {
            if (ModelState.IsValid)
            {
                mvnew.dateModified = DateTime.Now;
                db.mvNews.Attach(mvnew);
                db.ObjectStateManager.ChangeObjectState(mvnew, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.userId = new SelectList(db.mvUsers, "id", "username", mvnew.userId);
            return View(mvnew);
        }

        public ActionResult Delete(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            return View(mvnew);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            mvNew mvnew = db.mvNews.Single(m => m.id == id);
            db.mvNews.DeleteObject(mvnew);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public Guid AsyncUpload()
        {
            Guid g =  _fileStore.SaveUploadedFile(Request.Files[0]);
            return g;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}